package com.fenghongzhang.day004;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private StandardGSYVideoPlayer gsy;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = (RecyclerView) findViewById(R.id.rv);

        //1.布局
//        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setLayoutManager(new MyLinear(this));

        //2.适配器
        List<VideoBean> list = new ArrayList<>();

        for (int i = 0; i < TestVideoUrl.videoUrls.length; i++) {
            list.add(new VideoBean());
        }

        MyAdapter myAdapter = new MyAdapter(R.layout.rv_layout, list);
        rv.setAdapter(myAdapter);

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE://停止状态:
                        RecyclerView.LayoutManager layoutManager = rv.getLayoutManager();
                        StandardGSYVideoPlayer standardGSYVideoPlayer = layoutManager.getChildAt(0).findViewById(R.id.gsy);
                        standardGSYVideoPlayer.startPlayLogic();
                        break;
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
